% PROCESS
% INPUT
% XTR - training data (n x d)
% XTV - validation data (nv x d)
% XTE - testing data (ne x d)
% settings - a cell array containing the following fields
%	background - 0 or 1, is background model being used?
%	rks - 0 or 1, using random kitchen sinks?
%		ftimes - number of times to multiply features (used only with if rks == 1)
%	pca - 0 or 1, using pca?
%   	u - projection matrix (d x d95) (used only if pca == 1 AND projection matrix already computed)
%		m - feature mean vector (1 x d) (used only if pca == 1 AND projection matrix already computed)
%
% OUTPUT
% XTR - updated XTR
% XTV - updated XTV
% XTE - updated XTE
% settings - updated settings
function [XTR,XTV,XTE,settings] = process(XTR,XTV,XTE,settings)
	
	if settings.pca
		if isempty(settings.u)
			[U, ~, D] = princomp(XTR);
			d95       = find(cumsum(D)/sum(D) >= 0.95,1,'first');
			U         = U(:,1:d95);
			mXTR      = mean(XTR,1);
			XTR       = bsxfun(@minus, XTR, mXTR)*U;
			XTV       = bsxfun(@minus, XTV, mXTR)*U;
			XTE       = bsxfun(@minus, XTE, mXTR)*U;
			settings.u = U;
			settings.m = mXTR;
		else
			XTR       = bsxfun(@minus, XTR, settings.m)*settings.u;
			XTV       = bsxfun(@minus, XTV, settings.m)*settings.u;
			XTE       = bsxfun(@minus, XTE, settings.m)*settings.u;
		end
	end
	
	addpath(genpath('random-features'));
	if settings.rks
		[~, d] = size(XTR);
		XTRd = XTR./(d^0.5);
		[XTR,theta] = get_random_sinks(XTRd', d*settings.ftimes, 'fourier', 'gaussian');

		XTVd = XTV./(d^0.5);
		XTV = rp_apply(XTEV', theta);

		XTEd = XTE./(d^0.5);
		XTE = rp_apply(XTEd', theta);
	end
	
	
end